package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceSaveParam;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询围栏信息业务:geofenceQuery={}", geofenceQuery);
        List<GeofenceVO> geofenceVOList = geofenceMapper.selectGeofence(geofenceQuery);
        // 之前未对围栏的车辆信息进行处理 现在需要进行业务补充
        // 1.遍历围栏集合 依次给每一个围栏补充车辆信息
        for (int i = 0; i < geofenceVOList.size(); i++) {
            // 2.依次取出每个围栏对象
            GeofenceVO geofenceVO = geofenceVOList.get(i);
            // 3.封装车辆查询对象 查询条件: 围栏id
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            // 4.调用车辆数据层 直接根据条件查询当前围栏下的所有的车辆
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            // 5.获取当前围栏绑定车辆总数
            int totalNum = vehicleVOList.size();
            // 6.定义变量保存可用车数量
            int availableNum = 0;
            // 7.依次遍历每一辆车 如果车辆状态为1(空闲) 可用车辆数+1
            for (VehicleVO vehicleVO : vehicleVOList) {
                if (vehicleVO.getStatus().equals("1")) {
                    availableNum++;
                }
            }
            if (availableNum > 0) {
                geofenceVO.setAvailableNum(availableNum);
            } else {
                geofenceVO.setAvailableNum(0);
            }
            geofenceVO.setTotalNum(totalNum);
            // 因为后续需要查看当前围栏上所有车辆的详细信息 所以把查出来的车辆列表也赋值给围栏对象
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return geofenceVOList;
    }

    @Override
    public void saveGeofence(GeofenceSaveParam geofenceSaveParam) {
        log.debug("保存围栏信息业务:geofenceSaveParam={}", geofenceSaveParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceSaveParam, geofence);
        if (geofenceSaveParam.getId() == null) {
            geofence.setCreateTime(new Date());
            geofence.setStatus("1");
            geofenceMapper.saveNewGeofence(geofence);
        } else {
            geofence.setUpdateTime(new Date());
            geofenceMapper.updateGeofence(geofence);
        }
    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除围栏业务:id={}", id);
        // 需要判断该围栏是否有绑定车辆 若存在绑定车辆 则不执行删除操作
        // 1.封装车辆查询的DTO层对象
        // 2.调用车辆查询接口 查询该围栏下的车辆列表
        // 3.若该围栏存在绑定车辆 业务层对外抛出操作失败的异常
        // 4.若该围栏不存在绑定车辆 则正常执行删除操作
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
        if (vehicleVOList.size() > 0 && vehicleVOList != null) {
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        } else {
            geofenceMapper.deleteGeofence(id);
        }
    }
}
